package com.qsign.sfrz_android.base;

import android.app.Application;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Environment;
import com.qsign.sfrz_android.utils.ToolUtils;
import com.qsign.sfrz_android.utils.UpdateUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreater;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreater;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.tencent.bugly.crashreport.CrashReport;
import java.lang.reflect.Field;
import androidx.multidex.MultiDex;
import com.qsign.sfrz_android.R;

/**
 * Created by Administrator on 2018/7/24.
 */
public class MyApplication extends Application {
    public static String  mainfilepath= Environment.getExternalStorageDirectory()+"/";//主文件目录 暂时存储在sd卡
    public static String  rootpath=mainfilepath+"xqb/temp";
    public static String  pdftempname="fqgz.pdf";
    public static String  Userface="userface.jpg";
    public static String  tempmyimg="tempmy.jpg";
    public static String handOriginName = "handOrigin.png";
    public static Typeface typeFace;
    private static MyApplication instances;
    public static String signphotofath = "jsgzhn";
    public static MyApplication getInstance() {
        return instances;
    }
    static {//static 代码段可以防止内存泄露
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                layout.setPrimaryColorsId(R.color.allTransparent, R.color.color333333);//全局设置主题颜色
                return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);//指定为经典Header，默认是 贝塞尔雷达Header
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate);
            }
        });
    }
                                      //构建完成
    @Override
    public void onCreate() {
        super.onCreate();
        String processName = ToolUtils.getProcessName(this, android.os.Process.myPid());
        if(processName.equals(this.getApplicationContext().getPackageName())){//在应用主进程中才初始化 其他进程不初始化
            setTypeface();
            instances=this;
            //init bugly
            CrashReport.initCrashReport(getApplicationContext(), "91d107d56a", false);

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(3000);
                        UpdateUtils.checkUpload(instances);
                    }catch (Exception e){}

                }
            }).start();



    }
}

    public void setTypeface() {
        typeFace = Typeface.createFromAsset(getAssets(), "fonts/PingFang Regular.ttf");
        try {
            //与values/styles.xml中的<item name="android:typeface">sans</item>对应
            Field field = Typeface.class.getDeclaredField("SERIF");
            field.setAccessible(true);
            field.set(null, typeFace);

            Field field_1 = Typeface.class.getDeclaredField("DEFAULT");
            field_1.setAccessible(true);
            field_1.set(null, typeFace);

            //与monospace对应
            Field field_2 = Typeface.class.getDeclaredField("MONOSPACE");
            field_2.setAccessible(true);
            field_2.set(null, typeFace);

            //与values/styles.xml中的<item name="android:typeface">sans</item>对应
            Field field_3 = Typeface.class.getDeclaredField("SANS_SERIF");
            field_3.setAccessible(true);
            field_3.set(null, typeFace);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        // you must install multiDex whatever tinker is installed!
        MultiDex.install(base);
    }

}
